package com.jiukuaidao.zhulin.module.address;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jiukuaidao.library.base.BaseActivity;
import com.jiukuaidao.library.util.ToastUtil;
import com.jiukuaidao.zhulin.AppContext;
import com.jiukuaidao.zhulin.R;
import com.jiukuaidao.zhulin.data.TaskProvider;
import com.jiukuaidao.zhulin.entity.ApiCityListInfo;
import com.jiukuaidao.zhulin.entity.ApiHistoryAddressInfo.AddressInfo;
import com.jiukuaidao.zhulin.module.address.bean.EventAddressBean;
import com.jiukuaidao.zhulin.module.address.bean.EventGainAddressBean;
import com.jiukuaidao.zhulin.module.address.bean.PreAddressBean;
import com.jiukuaidao.zhulin.module.address.contract.AddAddressContract;
import com.jiukuaidao.zhulin.module.address.contract.CityListContract;
import com.jiukuaidao.zhulin.module.address.presenter.AddAddressPresenter;
import com.jiukuaidao.zhulin.module.address.presenter.CityListPresenter;
import com.jiukuaidao.zhulin.util.AddressInfoUtil;
import com.ta.utdid2.android.utils.StringUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;

/**
 * Author : PaiTaSuo
 * Date : 2016/5/30
 * Description :
 */
public class AddAddressActivity extends BaseActivity<AddAddressContract.Presenter> implements AddAddressContract.View, View.OnClickListener, CityListContract.View {

    @BindView(R.id.titile_left_imageview)
    ImageView mLeftImg;
    @BindView(R.id.titile_text)
    TextView mTitileText;
    @BindView(R.id.titile_right_text)
    TextView mRightText;
    @BindView(R.id.tv_select_address)
    TextView mSelectAddress;
    @BindView(R.id.tv_select_city)
    TextView mSelectCity;
    @BindView(R.id.et_house_numbers)
    EditText mEtHouseNumbers;
    @BindView(R.id.number)
    LinearLayout number;
    @BindView(R.id.et_phones)
    EditText mEtPhones;
    @BindView(R.id.et_name)
    EditText mEtName;
    private String mFromModel;
    private AddressInfo mAddressInfo;
    private ApiCityListInfo mCityListInfo;
    private CityListPresenter mCityPresenter;
    private CityListBottomDialog mPopup;
    private EventAddressBean mAddressBean;
    private String mCodeStr; // pop中城市ID 是否有变化
    private String mLongitude;//
    private String mLatitude;
    private String mAddressId = "0";
    private String mShopId = "0";


    public static AddAddressActivity mInstance;

    @Override
    protected View getTargetView() {
        return null;
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_add_address;
    }

    @Override
    protected void handleBundle(Bundle bundle) {
        mFromModel = bundle.getString("fromModel");

        if (mFromModel.equals("editAddress")) {
            mAddressInfo = (AddressInfo) bundle.getSerializable("AddressInfo");
        }

    }

    @Override
    protected void init() {
        mInstance = this;
        EventBus.getDefault().register(this);
        if (mFromModel.equals("addAddress") || mFromModel.equals("changeAddress") || mFromModel.equals("shopCart")) {
            mTitileText.setText("新增地址");
            mCodeStr = "";
        }
        if (mFromModel.equals("editAddress")) {
            mTitileText.setText("编辑地址");
        }
        mRightText.setText("保存");

        if (mAddressInfo != null) {
            mSelectCity.setText(mAddressInfo.city);
            mSelectAddress.setText(mAddressInfo.address);
            mEtHouseNumbers.setText(mAddressInfo.street);
            mEtPhones.setText(mAddressInfo.mobile);
            mEtName.setText(mAddressInfo.consignee);
            // 请求接口需要的参数
            mCodeStr = mAddressInfo.baiduCityCode;
            mLongitude = mAddressInfo.longitude;
            mLatitude = mAddressInfo.latitude;
            mAddressId = mAddressInfo.addressId;

        }
        setOnClickListener();
        mCityPresenter = new CityListPresenter(TaskProvider.provideTasksRepository(this), this);
        mCityPresenter.loadData();

    }


    @Override
    public void onClick(View view) {
        Bundle bundle = new Bundle();
        switch (view.getId()) {
            case R.id.titile_left_imageview:
                finish();
                break;
            case R.id.tv_select_address:
                String city = mSelectCity.getText().toString();
                String select = AppContext.getContext().getString(R.string.select_city);
                if (StringUtils.isEmpty(city) || city.equals(select)) {
                    ToastUtil.show(AppContext.getContext().getString(R.string.select_city), Toast.LENGTH_SHORT);
                    return;
                }
                if (null != mAddressBean) {
                    bundle.putString("region", mAddressBean.cityName);
                    bundle.putString("cityCode", mAddressBean.cityCode);
                } else {
                    bundle.putString("region", mAddressInfo.city);
                    bundle.putString("cityCode", mAddressInfo.baiduCityCode);
                }
                intentJump(GainPlaceActivity.class, bundle);
                break;
            case R.id.tv_select_city:
                if (null != mCityListInfo.cityList && mCityListInfo.cityList.size() > 0) {
                    mPopup = new CityListBottomDialog(AddAddressActivity.this, mCityListInfo, mCodeStr);
                    mPopup.showDialog();
                } else {
                    ToastUtil.show("城市列表为空", Toast.LENGTH_SHORT);
                }
                break;
            case R.id.titile_right_text:
                mPresenter = new AddAddressPresenter(TaskProvider.provideTasksRepository(this), this);
                mPresenter.start();
                break;
        }
    }

    public void setOnClickListener() {
        mLeftImg.setOnClickListener(this);
        mSelectAddress.setOnClickListener(this);
        mSelectCity.setOnClickListener(this);
        mRightText.setOnClickListener(this);
    }

    @Override
    public AddressInfo getAddressInfo() {
        mAddressInfo = new AddressInfo();
        mAddressInfo.consignee = mEtName.getText().toString();
        mAddressInfo.mobile = mEtPhones.getText().toString();
        mAddressInfo.street = mEtHouseNumbers.getText().toString();
        mAddressInfo.address = mSelectAddress.getText().toString();
        mAddressInfo.baiduCityCode = mCodeStr;
        mAddressInfo.longitude = mLongitude;
        mAddressInfo.latitude = mLatitude;
        mAddressInfo.addressId = mAddressId;
        mAddressInfo.shopId = mShopId;
        return mAddressInfo;
    }

    @Override
    public void toast(String msg) {
        ToastUtil.show(msg, Toast.LENGTH_SHORT);
    }

    @Override
    public void intentJump(String shopId) {
        mShopId = shopId;
        if (mFromModel.equals("addAddress") || mFromModel.equals("editAddress")) {
            finish();
            // 刷新列表
        }
        if (mFromModel.equals("shopCart")) {
            //当前位置存入首选项
            setPreAddress();
            //发送地址消息
            EventBus.getDefault().post(getAddressInfo());
            AddAddressActivity.this.finish();
            AddressManagerActivity.mInstance.finish();
        }
        if (mFromModel.equals("changeAddress")) {
            if (ChangePositionActivity.mInstance != null) {
                //当前位置存入首选项
                setPreAddress();
                //发送地址消息
                EventBus.getDefault().post(getAddressInfo());
                ChangePositionActivity.mInstance.finish();
            }
            AddAddressActivity.this.finish();
        }
    }

    /**
     * 地址存入首选项中
     */
    public void setPreAddress() {
        AddressInfoUtil.cleanAddressInfo();
        PreAddressBean mAddressInfo = new PreAddressBean();
        mAddressInfo.longitude = getAddressInfo().longitude;
        mAddressInfo.latitude = getAddressInfo().latitude;
        mAddressInfo.city = getAddressInfo().city;
        mAddressInfo.cityCode = getAddressInfo().baiduCityCode;
        mAddressInfo.address = getAddressInfo().address;
        AddressInfoUtil.saveAddressInfo(mAddressInfo);
    }

    @Override
    public void cityList(ApiCityListInfo cityListInfo) {
        mCityListInfo = cityListInfo;
    }

    @Override
    public String cityCode() {
        return null;
    }

    @Override
    public void setTitle(String title) {

    }

    /**
     * 选择城市 接收消息
     */
    @Subscribe
    public void onEventEditCity(EventAddressBean addressBean) {
        mAddressBean = addressBean;
        mSelectCity.setText(addressBean.cityName);
        mSelectAddress.setText("");
        mEtHouseNumbers.setText("");
        mEtPhones.setText("");
        mEtName.setText("");
        mCodeStr = addressBean.cityCode;

    }

    /**
     * 点击送货地址 接收到的消息
     */
    @Subscribe
    public void onEventEditAddress(EventGainAddressBean addressBean) {
        mSelectAddress.setText(addressBean.name);
        mLongitude = addressBean.longitude;
        mLatitude = addressBean.latitude;
    }
}
